<template>
        <tr >
          <th>{{item.id}}</th>
          <th>{{item.name}}</th>
          <td>{{item.date | formatTime}}</td>
          <td>
            <button @click="delBook(item.id)">删除</button>
            <button @click="editBook(item.id)" >编辑</button>
          </td>
        </tr>
</template>

<script>
export default {
  props: ['item'],
  data () {
    return {

    }
  },
  filters: {
    formatTime (i) {
      var nd = new Date(i)
      var y = nd.getFullYear()
      var mm = nd.getMonth() + 1
      var d = nd.getDate()
      var h = nd.getHours()
      var m = nd.getMinutes()
      if (mm < 10) {
        mm = '0' + mm
      }
      if (d < 10) {
        d = '0' + d
      }
      if (h < 10) {
        h = '0' + h
      }
      if (m < 10) {
        m = '0' + m
      }
      return y + '-' + mm + '-' + d + ' ' + h + ':' + m
    }
  },
  created () {

  },

  methods: {
    async delBook (i) {
      try {
        const res = await this.$axios({
          url: '/books/' + i,
          method: 'DELETE'
        })
        if (res.data.status === 200) {
          alert('删除成功')
          this.$emit('getList')
        }
      } catch (err) {
        alert(err.message)
      }
    },
    editBook (i) {
      this.$emit('editBook', i)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
